<?php

    // +----------------------------------------------------------------------
    // | 后台主页控制器
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\admin;

    use app\model\ucenter\ActionLog as ActionLogModel;
    use app\model\admin\Menu;
	use app\model\admin\Version as VersionModel;
	use app\event\im\Chat;
	use app\event\ucenter\User as UserEvent;
    use app\event\ucenter\AuthGroup as AuthGroupEvent;
	use app\model\admin\Module as ModuleModel;
	use think\helper\Str;

	class Index extends Common
    {

        /**
         * 后台首页
         * @return mixed
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function index()
        {
            return $this->fetch();
        }

        public function layout()
        {
            return $this->fetch();
        }

        public function home()
        {
			$modules = ModuleModel::where('status',1)->where('is_install',1)->select();

			$quick[] = [
				'title'=>'站点设置',
				'link'=>'admin/SystemConfig/setting',
				'ico'=>'layui-icon-set',
			];
			$wait = [];
			$overview = [];
			$data_left = '';
			$data_right = '';
			foreach ($modules as $module) {
				$this->app->getRootPath();
				$file = $this->app->getRootPath().
					'app'.DIRECTORY_SEPARATOR.'controller'.DIRECTORY_SEPARATOR
					.$module['name']
					.DIRECTORY_SEPARATOR.'admin' . DIRECTORY_SEPARATOR. Str::studly('home') . '.php';
                if (is_file($file))
				{
					$name = '\app\controller\\'.$module['name'].'\\admin\\'.Str::studly('home');
					$controller = new $name;
					$data_left .= $controller->left();
					$data_right .= $controller->right();
					$overview = array_merge($controller->overview(),$overview);
					$quick = array_merge($controller->quick(),$quick);
					$wait = array_merge($controller->wait(),$wait);
				}

			}
			$this->assign('data_left',$data_left);
			$this->assign('data_right',$data_right);
			$this->assign('overview',$overview);
			$this->assign('wait',$wait);
			$this->assign('quick',$quick);
            $logins = ActionLogModel::where('action_id','in','1,5,2')
                ->limit(10)
                ->where('request_module','admin')
                ->order('id DESC')->select();

            $this->assign('logins',$logins);
			return $this->fetch();
        }

        /**
         * icon title list jump
         */
        public function menu()
        {
            $top_menus = Menu::where('status', 1)->where('pid', 0)->where('is_admin', 1)
                ->where('hide', 0)
                //                ->field('id,title,icon,controller')
                ->where('type', 1)
                ->order('sort ASC')
                ->select();
            $menus = [];
            foreach ($top_menus as $mkey => $top_menu) {

                    $menu = [
                        'icon' => 'layui-icon-' . $top_menu['icon'],
                        // 模块名称
                        'name' => 'id' . $top_menu['module'],
                        'title' => $top_menu['title'],
                    ];
                    $groups = Menu::where('group', '<>', '')->where('pid', $top_menu['id'])
                        ->where('hide', 0)->where('type', 1)->where('status', 1)
                        ->distinct(true)
//                        ->order('sort')
                        ->column('group');
                    if ($groups) {
                        foreach ($groups as $key => $group) {
                            $children = [
                                'title' => $group,
                                'icon' => 'folder-open-o',
                            ];

                            // 先获取分组
                            $list_menus = Menu::where('group', $group)
                                ->where('pid', $top_menu['id'])->where('hide', 0)->where('status', 1)->order('sort')->select();

                            if ($list_menus) {
                                foreach ($list_menus as $keys => $lmenu) {
                                    if (UserEvent::isAdministrator($this->user) || true === AuthGroupEvent::checkRule($lmenu['url'], $this->user)) {
                                        $temp = [];
                                        $temp['name'] = 'id' . $lmenu['id'];
                                        //                                        $temp['target'] = 'navtab';
                                        $temp['title'] = $lmenu['title'];
                                        $temp['icon'] = 'layui-icon-' . ($lmenu['icon'] ?: 'list');
                                        if ($lmenu['param']) {
                                            $temp['jump'] = $lmenu['controller'] . '/' . $lmenu['action'] . '?' . $lmenu['param'];
                                        } else {
                                            $temp['jump'] = $lmenu['controller'] . '/' . $lmenu['action'];
                                        }
                                        $children['list'][] = $temp;
                                    }
                                }
                            }
                            if (!empty($children['list'])) {
                                $menu['list'][] = $children;
                            }
                        }
                    }
                    // 无分组
                    $temps = Menu::where('pid', $top_menu['id'])->where('group', '')
                        ->where('status', 1)
                        ->where('hide', 0)
                        ->where('type', 1)
                        ->order('sort')
                        ->select();
                    foreach ($temps as $key => $tmenu) {
                        if (UserEvent::isAdministrator($this->user) || true === AuthGroupEvent::checkRule($tmenu['url'], $this->user)) {
                            // 权限
                            if (159 == $tmenu['id']) {
                                $jump = '/';
                            } else {
                                if ($tmenu['param']) {
                                    $jump = $tmenu['controller'] . '/' . $tmenu['action'] . '?' . $tmenu['param'];
                                } else {
                                    $jump = $tmenu['controller'] . '/' . $tmenu['action'];
                                }

                                $jump = $tmenu['controller'] . '/' . $tmenu['action'];
                            }
                            $menu['list'][] = [
                                'name' => 'id' . $tmenu['id'],
                                'title' => $tmenu['title'],
                                'icon' => 'layui-icon-' . ($tmenu['icon'] ?: 'list'),
                                //                                'id' => 'id' . $tmenu['id'],
                                'jump' => $jump
                            ];
                        }
                    }
                    $menus[] = $menu;
                }
            $result = [];
            $result['code'] = 0;
            $result['data'] = $menus;
            return $this->json($result);
        }


    }
